<%
   new_prefix = normal_method_name(sprintf("%s_%s", prefix, property.out_name))
   func_def = sprintf("expand_%s(d, array_index):", new_prefix)
-%>
<% if !property.to_request.nil? -%>


def <%= func_def %>
<%= indent(lines(property.to_request, -1), 4) -%>
<% elsif property.is_a?(Api::Type::Array) && property.item_type.is_a?(Api::Type::NestedObject) -%>


def <%= func_def %>
    new_ai = dict()
    if array_index:
        new_ai.update(array_index)

    req = []

<%   if property.array_num.nil? -%>
<%     raise "no field for parameter: " + property.name if property.field.nil? -%>
<%     rp = find_property(resource, property.field.split("."))
       unless rp.is_a?(Api::Type::Array)
         raise "parameter(#{property.name}) is an array, but the matched property is not"
       end
-%>
    v = navigate_value(
        d, [<%= index2navigate(property.field, true) %>], new_ai)

    if not v:
        return req
    n = len(v)<%# v may be None %>
<%   else -%>
    n = <%= property.array_num %>
<%   end -%>
    for i in range(n):
<%   unless property.field.nil? -%>
        new_ai["<%= to_schema_index(property.field) %>"] = i
<%   end -%>
        transformed = dict()

<%   nested_properties = nested_properties(property) -%>
<%= indent(lines(build_expand_properties(nested_properties, "d, new_ai", new_prefix, "transformed")), 4) %>

        if transformed:
            req.append(transformed)

    return req
<%   nested_properties.each do |prop| -%>
<%= lines(build_expand_method(resource, new_prefix, prop)) -%>
<%   end -%>
<% elsif property.is_a?(Api::Type::NestedObject) -%>


def <%= func_def %>
    r = dict()

<%   nested_properties = nested_properties(property) -%>
<%= lines(build_expand_properties(nested_properties, "d, array_index", new_prefix, "r")) -%>

    return r
<%   nested_properties.each do |prop| -%>
<%= lines(build_expand_method(resource, new_prefix, prop)) -%>
<%   end -%>
<% end -%>
